# Makefile for Digital Envelope Project

# 编译器
CC = gcc

# GmSSL路径
GMSSL_INCLUDE = /root/GmSSL/include
GMSSL_LIB = /root/GmSSL/build/bin

# 编译选项
CFLAGS = -Wall -O2 -I $(GMSSL_INCLUDE)
LDFLAGS = -L $(GMSSL_LIB) -lgmssl

# 所有目标
ALL_TARGETS = alice_keygen bob_keygen alice_envelope bob_decrypt

.PHONY: all clean step1 step2 step3 step4 step5 step6

all: $(ALL_TARGETS)

# Alice密钥生成
alice_keygen: alice_keygen.c
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

# Bob密钥生成
bob_keygen: bob_keygen.c
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

# Alice数字信封
alice_envelope: alice_envelope.c
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

# Bob解密验证
bob_decrypt: bob_decrypt.c
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

# 清理
clean:
	rm -f $(ALL_TARGETS) *.o *.bin *.txt digital_envelope.bin

# 分步执行
step1: alice_keygen bob_keygen
	./alice_keygen
	./bob_keygen

step2:
	cp alice_public_key.bin ./
	cp bob_public_key.bin ./

step3:
	echo "20231414wsq" > plain.txt

step4: alice_envelope
	./alice_envelope

step5: bob_decrypt
	./bob_decrypt

step6:
	@echo "原始明文:"
	@cat plain.txt
	@echo -e "\n解密后的明文:"
	@cat decrypted_plain.txt
	@diff -q plain.txt decrypted_plain.txt && echo "✓ 验证成功" || echo "✗ 验证失败"

test:
	@echo "测试GmSSL安装:"
	@gmssl version || echo "GmSSL未安装"
	@echo "测试编译环境:"
	@$(CC) --version || echo "GCC未安装"
